using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GTA
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Font to write text
        public static SpriteFont font;

        // Height and Width of the game screen
        public const int WIDTH = 800, HEIGHT = 600;

        public enum GameState
        {
            Menu,
            Ingame,
            Pause
        }
        // Current state of the game
        public static GameState State = GameState.Menu;

        /***********************\
              GAME ELEMENTS     
        \***********************/

        // Menu of the game
        public Menu Menu
        {
            get { return menu; }
            set { menu = value; }
        }
        Menu menu = new Menu();
        // Player character
        public Player Player
        {
            get { return player; }
            set { player = value; }
        }
        Player player;    
        // Background of the Game
        public Background Background
        {
            get { return background; }
            set { background = value; }
        }
        Background background = new Background();
        // Building
        public List<Building> BuildingList
        {
            get { return buildingList; }
            set { buildingList = value; }
        }
        List<Building> buildingList = new List<Building>();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Sets the dimensions of the game screen
            graphics.PreferredBackBufferHeight = HEIGHT;
            graphics.PreferredBackBufferWidth = WIDTH;

            // Allow to display the mouse on the screen game
            IsMouseVisible = true;

        }

        protected override void Initialize()
        {
            player = new Player(this);

            buildingList.Add(new Building(new Vector2(100, 100)));
            buildingList.Add(new Building(new Vector2(400, 100)));
            buildingList.Add(new Building(new Vector2(200, 500)));
            buildingList.Add(new Building(new Vector2(600, 550)));

            base.Initialize();
        }


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            font = Content.Load<SpriteFont>("Fonts/debug");
            menu.InitMenu(Content);
            player.InitPlayer(Content);
            background.InitBackground(Content);
            foreach (Building item in buildingList)
                item.InitBuilding(Content);

        }


        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Y))
                MediaPlayer.Volume -= 0.005f;
            if (Keyboard.GetState().IsKeyDown(Keys.U))
                MediaPlayer.Volume += 0.005f;
            // Pause and menu update
            if (State != GameState.Ingame)
                menu.Update(this);
            // Game update
            else
            {
                player.Update(background);
                background.Update();
                foreach (Building item in buildingList)
                    item.Update();
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            // Pause and menu update
            if (State != GameState.Ingame)
                menu.DrawMenu(spriteBatch);
            // Game update
            else
            {
                background.DrawBackground(spriteBatch);
                player.DrawPlayer(spriteBatch);
                foreach (Building item in buildingList)
                    item.DrawBuilding(spriteBatch);
            }

            spriteBatch.DrawString(font, "lol : " + MediaPlayer.Volume, new Vector2(0, 20), Color.Black);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        /* Custom functions */

    }
}
